package com.example.liunanxin.lnx226ordering.Fragment;


import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.liunanxin.lnx226ordering.R;
import com.example.liunanxin.lnx226ordering.activities.FoodActivity;
import com.example.liunanxin.lnx226ordering.adapter.FoodAdapter;
import com.example.liunanxin.lnx226ordering.adapter.ShopAdapter;
import com.example.liunanxin.lnx226ordering.beans.AllShopsBean;
import com.example.liunanxin.lnx226ordering.iface.ShopListListener;
import com.example.liunanxin.lnx226ordering.model.ShopModel;

import java.util.List;

public class HomeFragment extends Fragment {
    private RecyclerView recyclerView;
    private List<AllShopsBean> shopsBeans;
    private ShopAdapter shopAdapter;

    public HomeFragment(){

    }

//    private ShopListener shopListener=new ShopListener() {
////
////        @Override
////        public void onResponse(List<AllShopsBean> shopsBean) {
////            shopsBeans=shopsBean;
////            if (shopsBeans==null)
////                Toast.makeText(getContext(),"null",Toast.LENGTH_LONG).show();
////            else{
//////                Toast.makeText(getContext(),shopsBeans.get(0).getShopname(),Toast.LENGTH_LONG).show();
////                shopAdapter.setData(shopsBeans);
////
////            }
////        }

    //        @Override
//        public void onFail(String mag) {
//            Toast.makeText(getContext(),mag,Toast.LENGTH_LONG).show();
//        }
//    };
    private ShopListListener shopListListener=new ShopListListener() {
        @Override
        public void onResponse(List<AllShopsBean> mshopsBeans) {
            shopsBeans=mshopsBeans;
            if (shopsBeans==null)
                Toast.makeText(getContext(),"null",Toast.LENGTH_LONG).show();
            else{
                Toast.makeText(getContext(),shopsBeans.get(0).getShopname(),Toast.LENGTH_LONG).show();
                shopAdapter.setDate(shopsBeans);

            }
        }

        @Override
        public void onFail(String mag) {

        }
    };

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        ShopModel shopModel=new ShopModel();
        shopModel.getShopResult(shopListListener);
        initRecyclerview(view);
        click();
    }

    public void initRecyclerview(View view){
        recyclerView=view.findViewById(R.id.recycler);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.addItemDecoration(new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL));
        shopAdapter=new ShopAdapter();
        recyclerView.setAdapter(shopAdapter);
    }

    public void click(){
        shopAdapter.setOnItemClickListener(new ShopAdapter.OnItemClickListener() {
            @Override
            public void setItem(View view, int position) {
                Intent intent=new Intent(getActivity(),FoodActivity.class);
                intent.putExtra("shop_id",shopsBeans.get(position).getShop_id());
                intent.putExtra("shopname",shopsBeans.get(position).getShopname());
                startActivity(intent);
            }
        });
    }
}

